import os
import re
import  shutil
classes = ['ren']

p = re.compile(r'(.*)\.')
a=10
for i in os.listdir(r'D:\xunlian\custom_training\labels1'):
    i = p.findall(i)[0]

    print(i)
    old_image_path = os.path.join(r'D:\xunlian\custom_training\labels1', f'{i}.jpg')
    new_image_path = os.path.join(r'D:\xunlian\custom_training\labels2', f'{a}.jpg')
    old_image_path2 = os.path.join(r'D:\xunlian\custom_training\labels1', f'{i}.txt')
    new_image_path2 = os.path.join(r'D:\xunlian\custom_training\labels2', f'{a}.txt')
    print(f"Renaming {old_image_path} to {new_image_path}")

    try:
        shutil.copy2(old_image_path, new_image_path)
        shutil.copy2(old_image_path2, new_image_path2) # 使用 copy2 保留元数据
        a += 1
    except Exception as e:
        print(f"Failed to copy and rename ")